package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐
 */
@Slf4j
@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public boolean saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息
        this.save(setmealDto);

        //获取套餐中的菜品集合List，并在套餐菜品关系表中设置套餐的id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息
        boolean result = setmealDishService.saveBatch(setmealDishes);
        if (!result) {
            return false;
        }
        return true;
    }

    /**
     * 删除套餐，同时需要删除套餐和菜品的关联关系
     *
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐是否为停售状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);

        //如果不能删除，则抛出一个业务异常
        int count = (int) this.count(queryWrapper);
        if (count > 0) {
            throw new CustomException("套餐正在售卖中，不能删除！");
        }

        //如果可以删除，先删除套餐表中的数据 ---- setmeal
        this.removeByIds(ids);

        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        //再暴力删除套餐菜品关系表中的数据 ---- setmeal_dish
        setmealDishService.remove(lambdaQueryWrapper);
    }

    /**
     * 单个/批量停售套餐
     *
     * @param ids
     * @return
     */
    @Override
    public boolean updateStatusStopByIds(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = this.getById(id);
            setmeal.setStatus(0);
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getId, id);
            boolean result = this.update(setmeal, queryWrapper);
            if (!result) {
                return false;
            }
        }
        return true;
    }

    /**
     * 单个/批量起售套餐
     *
     * @param ids
     * @return
     */
    @Override
    public boolean updateStatusStartByIds(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = this.getById(id);
            setmeal.setStatus(1);
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getId, id);
            boolean result = this.update(setmeal, queryWrapper);
            if (!result) {
                return false;
            }
        }
        return true;
    }

    /**
     * 根据id查询套餐信息，菜品信息，用于回显数据
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //查询套餐信息获取套餐对象
        Setmeal setmeal = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();
        //拷贝setmeal对象的属性到setmealDto
        BeanUtils.copyProperties(setmeal, setmealDto);
        System.out.println(setmealDto);

        //查询当前套餐对应的菜品信息，从setmeal_dish中查询
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> dishes = setmealDishService.list(queryWrapper);

        //再把查询到的菜品信息set进setmealDto中，返回该对象
        setmealDto.setSetmealDishes(dishes);
        return setmealDto;
    }

    /**
     * 修改套餐信息，同时更新对应的套餐菜品关系
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //更新setmeal表的基本信息
        this.updateById(setmealDto);

        //清除当前套餐的菜品信息------setmeal_dish的delete操作
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //添加当前提交过来的菜品信息----setmeal_dish的insert操作
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        //这里除了setmeal_id没有设置进去，其他属性都设置进去了
        //遍历dishes来设置setmeal_id
        dishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(dishes);
    }
}
